package com.baway.biliclitent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baway.biliclitent.R;
import com.baway.biliclitent.model.RecommendBean;
import com.baway.biliclitent.util.GlideImageLoader;
import com.baway.biliclitent.view.MyGridView;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.List;

/**
 * 作者：朱振江 on 2017/3/10 0010 10:19
 * 邮箱：zzjdys@Gmail.com
 * 类用途:
 */


public class RecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<String> mbanner;

    private List<RecommendBean.ResultBean> mbean;

    //轮播图
    private static final int TYPE_Banner = 1;
    //热门焦点
    private static final int TYPE_Recommend = 2;
    //直播
    private static final int TYPE_Live = 3;
    //番剧推荐
    private static final int TYPE_Bangumi_2 = 4;
    //话题
    private static final int TYPE_Weblink = 6;
    //活动中心
    private static final int TYPE_Activity = 7;
    private int mHeaderCount=1;
    //头部View个数
    
    private String name=null;
    private int id=0;
    public RecommendAdapter(Context context, List<RecommendBean.ResultBean> mbean, List<String> mbanner) {
        mContext = context;
        this.mbean = mbean;
        this.mbanner = mbanner;
    }

    //图标
    private int[] icons = new int[]{
            R.drawable.ic_header_hot, R.drawable.ic_head_live,
            R.drawable.ic_category_t13, R.drawable.ic_category_t1,
            R.drawable.ic_category_t3, R.drawable.ic_category_t129,
            R.drawable.ic_category_t4, R.drawable.ic_category_t119,
            R.drawable.ic_category_t36, R.drawable.ic_category_t160,
            R.drawable.ic_category_t155, R.drawable.ic_category_t5,
            R.drawable.ic_category_t11, R.drawable.ic_category_t23
    };


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (TYPE_Banner == viewType) {
            view = LayoutInflater.from(mContext).inflate(R.layout.fragment_recommend_banner, parent, false);
            return new banner(view);
        } else if (TYPE_Recommend == viewType) {
            view = LayoutInflater.from(mContext).inflate(R.layout.fragment_recomment_recommend, parent, false);
            return new recommend(view);
        } else if (TYPE_Live == viewType) {
            view = LayoutInflater.from(mContext).inflate(R.layout.fragment_recommend_live, parent, false);
            return new live_(view);
        } else if (TYPE_Bangumi_2 == viewType) {
            view = LayoutInflater.from(mContext).inflate(R.layout.fragment_recommend_bangumi_2, parent, false);
            return new bangumi_2(view);
        } else if (TYPE_Weblink == viewType) {
            view = LayoutInflater.from(mContext).inflate(R.layout.fragment_recommend_weblink, parent, false);
            return new weblink(view);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.fragment_recommend_activity, parent, false);
            return new activity_(view);
        }
    }
    

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        String title = mbean.get(position).getHead().getTitle();
//        titleimage(holder,title);
        RecommendBean.ResultBean resultBean = mbean.get(position);
        List<RecommendBean.ResultBean.BodyBean> body = resultBean.getBody();
        
        
        if (holder instanceof banner)
        {
            banner banner= (RecommendAdapter.banner) holder;
            //设置图片加载器
                banner.mRecommend_banner.setImageLoader(new GlideImageLoader());
            //设置图片集合
            banner.mRecommend_banner.setImages(mbanner);
            banner.mRecommend_banner.setIndicatorGravity(BannerConfig.RIGHT);
            //banner设置方法全部调用完毕时最后调用
            banner.mRecommend_banner.start();
        }else if (holder instanceof recommend)
        {
            recommend recommend= (recommend) holder;
            if ("热门焦点".equals(title))
            {
                recommend.mRecommend_item_type_img.setImageResource(icons[0]);
            }
            
            recommend.mRecommend_item_type_tv.setText(title);
            if (id==0)
            {
                recommend.mRecommend_item_type_rank_btn.setVisibility(View.VISIBLE);
            }else 
            {
                recommend.mRecommend_item_recommend_refresh_layout.setVisibility(View.GONE);
                recommend.mRecommend_item_refresh_layout.setVisibility(View.VISIBLE);
                recommend.mRecommend_item_type_rank_btn.setVisibility(View.GONE);
            }
            
            recommend.mRecommend_item_dynamic.setText("198条新动态,点这里刷新");
            item_recommendadapter item_recommendadapter=new item_recommendadapter(mContext);
            item_recommendadapter.setData(body);
            id++;
            recommend.mRecommend_gridview.setAdapter(item_recommendadapter);
        }else if (holder instanceof live_)
        {
            
            live_ live= (live_) holder;
            if ("正在直播".equals(title))
            {
                live.mLive_item_type_img.setImageResource(icons[1]);
            }
            switch (title) {

                case "正在直播":
                    live.mLive_item_type_img.setImageResource(icons[1]);
                    break;
                case "动画区":
                    live.mLive_item_type_img.setImageResource(icons[3]);
                    break;
                case "音乐区":
                    live.mLive_item_type_img.setImageResource(icons[4]);
                    break;
                case "舞蹈区":
                    live.mLive_item_type_img.setImageResource(icons[5]);
                    break;
                case "游戏区":
                    live.mLive_item_type_img.setImageResource(icons[6]);
                    break;
                case "鬼畜区":
                    live.mLive_item_type_img.setImageResource(icons[7]);
                    break;
                case "科技区":
                    live.mLive_item_type_img.setImageResource(icons[8]);
                    break;
                case "生活区":
                    live.mLive_item_type_img.setImageResource(icons[9]);
                    break;
                case "时尚区":
                    live.mLive_item_type_img.setImageResource(icons[10]);
                    break;
                case "娱乐区":
                    live.mLive_item_type_img.setImageResource(icons[11]);
                    break;
                case "电视剧区":
                    live.mLive_item_type_img.setImageResource(icons[12]);
                    break;
                case "电影区":
                    live.mLive_item_type_img.setImageResource(icons[13]);
                    break;
            }
            live.mLive_item_dynamic.setText("198条新动态,点这里刷新");
            live.mLive_item_type_tv.setText(title);
            item_livedadapter item_livedadapter=new item_livedadapter(mContext);
            item_livedadapter.setData(body);
            live.mLive_gridview.setAdapter(item_livedadapter);
        }else if (holder instanceof bangumi_2)
        {
            bangumi_2 bangumi_2=(bangumi_2)holder;
            if ("番剧推荐".equals(title))
            {
                bangumi_2.mBangumi_item_type_img.setImageResource(icons[2]);
            }
            bangumi_2.mBangumi_item_type_tv.setText(title);
            item_bangumidadapter item_bangumidadapter=new item_bangumidadapter(mContext);
            item_bangumidadapter.setData(body);
            bangumi_2.mBangumi_gridview.setAdapter(item_bangumidadapter);
        }else if (holder instanceof weblink)
        {
            weblink weblink= (weblink) holder;
            if (body.size()==1)
            {
                String cover = body.get(0).getCover();
                Glide.with(mContext).load(cover).into(weblink.mWeb_topic_image);
                weblink.mWeb_topic_title.setText(body.get(0).getTitle());
            }else 
            {
                String cover = body.get(position).getCover();
                Glide.with(mContext).load(cover).into(weblink.mWeb_topic_image);
                weblink.mWeb_topic_title.setText(body.get(position).getTitle());
            }
        }else if (holder instanceof activity_)
        {
            activity_ activity_= (activity_) holder;
            activity_.mActivity_text.setText("活动中心");
        }
        
        
    }

   
    @Override
    public int getItemCount() {
        return mbean.size();
    }

    //内容长度 
    public int getContentItemCount()
    {
        return mbean.size(); 
    }
    
    //判断当前item是否是HeadView 
    public boolean isHeaderView(int position) {
        return mHeaderCount != 0 && position < mHeaderCount;
    }
    @Override
    public int getItemViewType(int position) {



        int dataItemCount = getContentItemCount();
        String type = mbean.get(position).getType();
        if (mHeaderCount != 0 && position < mHeaderCount) {
            //头部View 
            return TYPE_Banner;
        } 
            //内容View
            if ("activity".equals(type)) {
                return TYPE_Activity;
            } else if ("bangumi_2".equals(type)) {
                return TYPE_Bangumi_2;
            } else if ("weblink".equals(type)) {
                return TYPE_Weblink;
            } else if ("recommend".equals(type)){
                return TYPE_Recommend;
            }else
            {
                return TYPE_Live;
            }
        
    }
    
    
    
     class activity_ extends RecyclerView.ViewHolder {

//        private final RecyclerView mActivi_recycle;
        private  ImageView mActivity_center_type_img;
        private  TextView mActivity_center_type_tv;
        private  TextView mActivity_text;

        public activity_(View itemView) {
            super(itemView);
            mActivity_text = (TextView) itemView.findViewById(R.id.activity_text);
//            mActivi_recycle = (RecyclerView) itemView.findViewById(R.id.activi_recycle);
            mActivity_center_type_img = (ImageView) itemView.findViewById(R.id.activity_center_type_img);
            mActivity_center_type_tv = (TextView) itemView.findViewById(R.id.activity_center_type_tv);
        }
    }

     class bangumi_2 extends RecyclerView.ViewHolder {

        private final ImageView mBangumi_item_type_img;
        private final TextView mBangumi_item_type_tv;
        private final MyGridView mBangumi_gridview;
        private final ImageView mBangumi_item_btn_bangumi_timeline;
        private final ImageView mBangumi_item_btn_bangumi_index;

        public bangumi_2(View itemView) {
            super(itemView);
            mBangumi_item_type_img = (ImageView) itemView.findViewById(R.id.bangumi_item_type_img);
            mBangumi_item_type_tv = (TextView) itemView.findViewById(R.id.bangumi_item_type_tv);
            mBangumi_gridview = (MyGridView) itemView.findViewById(R.id.bangumi_gridview);
            mBangumi_item_btn_bangumi_timeline = (ImageView) itemView.findViewById(R.id.bangumi_item_btn_bangumi_timeline);
            mBangumi_item_btn_bangumi_index = (ImageView) itemView.findViewById(R.id.bangumi_item_btn_bangumi_index);
        }
    }

     class banner extends RecyclerView.ViewHolder {

        private final Banner mRecommend_banner;

        public banner(View itemView) {
            super(itemView);
            mRecommend_banner = (Banner) itemView.findViewById(R.id.Recommend_banner);
        }
    }

     class live_ extends RecyclerView.ViewHolder {

        private final ImageView mLive_item_type_img;
        private final TextView mLive_item_type_tv;
        private final MyGridView mLive_gridview;
        private final TextView mLive_item_dynamic;

        public live_(View itemView) {
            super(itemView);
            mLive_item_type_img = (ImageView) itemView.findViewById(R.id.live_item_type_img);
            mLive_item_type_tv = (TextView) itemView.findViewById(R.id.live_item_type_tv);
            mLive_gridview = (MyGridView) itemView.findViewById(R.id.live_gridview);
            mLive_item_dynamic = (TextView) itemView.findViewById(R.id.live_item_dynamic);
        }
    }

     class weblink extends RecyclerView.ViewHolder {

        private final ImageView mWeb_topic_image;
        private final TextView mWeb_topic_title;

        public weblink(View itemView) {
            super(itemView);
            mWeb_topic_image = (ImageView) itemView.findViewById(R.id.web_topic_image);
            mWeb_topic_title = (TextView) itemView.findViewById(R.id.web_topic_title);
        }
    }

     class recommend extends RecyclerView.ViewHolder {

        private final ImageView mRecommend_item_type_img;
        private final TextView mRecommend_item_type_tv;
        private final TextView mRecommend_item_type_rank_btn;
        private final MyGridView mRecommend_gridview;
         private final TextView mRecommend_item_dynamic;
         private final LinearLayout mRecommend_item_refresh_layout;
         private final LinearLayout mRecommend_item_recommend_refresh_layout;

         public recommend(View itemView) {
            super(itemView);
            mRecommend_item_type_img = (ImageView) itemView.findViewById(R.id.recommend_item_type_img);
            mRecommend_item_type_tv = (TextView) itemView.findViewById(R.id.recommend_item_type_tv);
            mRecommend_item_type_rank_btn = (TextView) itemView.findViewById(R.id.recommend_item_type_rank_btn);
            mRecommend_gridview = (MyGridView) itemView.findViewById(R.id.recommend_gridview);
             mRecommend_item_dynamic = (TextView) itemView.findViewById(R.id.recommend_item_dynamic);
             mRecommend_item_refresh_layout = (LinearLayout) itemView.findViewById(R.id.recommend_item_refresh_layout);
             mRecommend_item_recommend_refresh_layout = (LinearLayout) itemView.findViewById(R.id.recommend_item_recommend_refresh_layout);
         
         
         }
    }


    
}
